package cn.iocoder.yudao.module.gateway.service.deviceconfig;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.gateway.controller.admin.deviceconfig.vo.*;
import cn.iocoder.yudao.module.gateway.dal.dataobject.deviceconfig.DeviceConfigDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.gateway.dal.mysql.deviceconfig.DeviceConfigMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.gateway.enums.ErrorCodeConstants.*;

/**
 * 设备配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DeviceConfigServiceImpl implements DeviceConfigService {

    @Resource
    private DeviceConfigMapper deviceConfigMapper;

    @Override
    public Long createDeviceConfig(DeviceConfigSaveReqVO createReqVO) {
        // 插入
        DeviceConfigDO deviceConfig = BeanUtils.toBean(createReqVO, DeviceConfigDO.class);
        deviceConfigMapper.insert(deviceConfig);

        // 返回
        return deviceConfig.getId();
    }

    @Override
    public void updateDeviceConfig(DeviceConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceConfigExists(updateReqVO.getId());
        // 更新
        DeviceConfigDO updateObj = BeanUtils.toBean(updateReqVO, DeviceConfigDO.class);
        deviceConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceConfig(Long id) {
        // 校验存在
        validateDeviceConfigExists(id);
        // 删除
        deviceConfigMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceConfigListByIds(List<Long> ids) {
        // 删除
        deviceConfigMapper.deleteByIds(ids);
        }


    private void validateDeviceConfigExists(Long id) {
        if (deviceConfigMapper.selectById(id) == null) {
            throw exception(DEVICE_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public DeviceConfigDO getDeviceConfig(Long id) {
        return deviceConfigMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceConfigDO> getDeviceConfigPage(DeviceConfigPageReqVO pageReqVO) {
        return deviceConfigMapper.selectPage(pageReqVO);
    }

}